<template>
	<view class="page index">
		<view class="form">
			<common-input 
			  title="任务名称" 
				placeholder="请填写任务名称" 
				v-model="title"
				:border-bottom="false"
			>
			</common-input>
			<picker mode="selector" :value="progressIndex" :range="progressList" @change="selectProgress">
				<view class="picker">
					<common-input 
					  title="任务优先级" 
					  placeholder="请选择任务优先级" 
					  v-model="progressIndex < 0 ? '' : progressList[progressIndex]" 
					  :require="false" 
					  :select="true" 
					  :disabled="true" 
					  :borderBottom="false"
					>
					</common-input>
				</view>
			</picker>
			<!-- <common-input
			  title="任务负责人" 
				placeholder="请选择人员" 
				v-model="head"
				:disabled="true"
				:border-bottom="false"
			>
			</common-input> -->
			<view class="picker" @click="showSelect = true">
				<common-input
				  title="任务参与人" 
					placeholder="请选择人员" 
					v-model="participants"
					:disabled="true"
					:border-bottom="false"
				>
				</common-input>
			</view>
			<picker mode="date" @change="selectEndTime">
				<view class="picker">
					<common-input
					  title="任务截止日期"
						placeholder="请填写截止日期"
						v-model="endTime"
						:disabled="true"
					>
					</common-input>
				</view>
			</picker>
			<view class="text">
				<view class="title">
					<view>
						任务详细说明
					</view>
					<view class="require"></view>
				</view>
				<textarea v-model="taskExplain" placeholder="请输入" placeholder-class="place"></textarea>
			</view>
			<picker mode="selector" :range="customerList" @change="selectCustomer">
				<view class="picker">
					<common-input
					  title="关联客户" 
						placeholder="请选择" 
						v-model="customerIndex < 0 ? '' : customerList[customerIndex]"
						:boder-top="false"
						:require="false"
						:disabled="true"
					>
					</common-input>
				</view>
			</picker>
			<picker mode="selector" :range="projectList" @change="selectProject">
				<view class="picker">
					<common-input
					  title="关联项目" 
						placeholder="请选择" 
						v-model="projectIndex < 0 ? '' : projectList[projectIndex]"
						:boder-top="false"
						:require="false"
						:disabled="true"
					>
					</common-input>
				</view>
			</picker>
			<update-file @update="updatefiles"></update-file>
		</view>
		<view class="footer">
			<button @click="save">保存</button>
		</view>
		<common-select :show="showSelect" :list="userList" @changePopup="changeShow" @select="select"></common-select>
	</view>
</template>

<script>
	import commonInput from "@/components/commonInput.vue"
	import commonSelect from "@/components/commonSelect.vue";
	import updateFile from "@/components/updateFile.vue"
	import {getUserListApi, getCustomerListApi} from "@/api/common.js";
	import {createTaskApi} from "@/api/task_api.js";
	import {getProjectListApi} from "@/api/project_api.js";
	export default{
		data() {
			return{
				progressIndex:0,
				progressList:['极高', '高', '中', '低'],
				title:'',
				participants:'',//参与人
				participantIds: [],
				userList: [],
				showSelect: false,
				endTime:'',
				taskExplain:'',
				customerIndex: -1,
				customerList: ['暂无客户，请先创建客户'],
				customerId: [],
				projectIndex: -1,
				projectList: ['请先选择客户'],
				projectId: [],
				fileUrls:[]
			}
		},
		components: {
			commonInput,
			updateFile,
			commonSelect
		},
		onLoad() {
			this.getUserList();
			this.getCustomerList();
		},
		methods:{
			// 获取用户列表
			getUserList(){
				getUserListApi().then((res) => {
					res.data = res.data.map((item) => {
						let value = item.id;
						let name = item.nickname;
						return {
							value,
							name
						}
					})
					this.userList = res.data;
				})
			},
			changeShow(e){
				this.showSelect = e;
			},
			select(e){
				this.participants = e.map((item) => item.name).join(',');
				this.participantIds = e.map((item) => item.value);
			},
			// 获取客户列表
			async getCustomerList(){
				let res = await getCustomerListApi();
				this.customerList = res.data.map((item) => item.name);
				this.customerId = res.data.map((item) => item.id);
			},
			selectCustomer(e){
				if(this.customerId.length == 0){
					uni.navigateTo({
						url:'/pages/index/customer/createCustomer'
					})
					return;
				}
				if(this.customerIndex == e.detail.value){
					return;
				}
				this.customerIndex = e.detail.value;
				this.getProjectList();
			},
			// 获取项目列表
			async getProjectList(){
				let res = await getProjectListApi({
					customerId: this.customerId[this.customerIndex]
				})
				this.progressList = res.data.list.map(item => item.name);
				this.projectId = res.data.list.map(item => item.id);
				if(this.progressList.length == 0){
					this.progressList = ['该客户还没有项目']
				}
			},
			selectProject(e){
				this.projectIndex = e.detail.value;
			},
			selectProgress(e){
				this.progressIndex = e.detail.value;
			},
			selectEndTime(e){
				this.endTime = e.detail.value;
			},
			updatefiles(e){
				this.fileUrls = e.map(item => item.path)
			},
			save(){
				if(this.title == ''){
					uni.showToast({
						icon: 'none',
						title: '任务名称不能为空'
					})
					return;
				}
				if(this.participantIds.length == 0){
					uni.showToast({
						icon: 'none',
						title: '请选择任务参与人'
					})
					return;
				}
				if(this.taskExplain == ''){
					uni.showToast({
						icon: 'none',
						title: '请填写任务说明'
					})
					return;
				}
				if(this.endTime == ''){
					uni.showToast({
						icon: 'none',
						title: '截止日期不能为空'
					})
					return;
				}
				if(this.customerId.length == 0){
					this.customerIndex = -1;
				}
				if(this.projectId.length == 0){
					this.projectIndex = -1;
				}
				uni.showLoading({
					mask: true,
					title: '加载中...'
				})
				createTaskApi({
					name: this.title,
					priority: this.progressIndex,
					deadLine: new Date(this.endTime).getTime(),
					description: this.taskExplain,
					customerId: this.customerIndex < 0 ? '' : this.customerId[this.customerIndex],
					projectId: this.projectIndex < 0 ? '' : this.projectId[this.projectIndex],
					involveUserIds: this.participantIds,
					fileUrls: this.fileUrls
				}).then(() => {
					uni.hideLoading();
					uni.showToast({
						icon: 'success',
						title: '创建成功'
					})
					// setTimeout(() => {
					// 	uni.navigateBack();
					// },1500)
					uni.navigateBack();
				}).catch(() => {
					uni.hideLoading()
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/common/style/customicons.scss";
	.index{
		padding: 24rpx 0;
		@include flex-c;
		.form{
			flex: 1 0 0;
			overflow: auto;
			.text{
				border-bottom: 4rpx #e5e5e5 solid;
				padding: 12rpx 30rpx;
				.title{
					font-size: 28rpx;
					color: #101010;
					padding: 0 0 16rpx;
					display: flex;
					align-items: center;
					.require{
					  height: 28rpx;
					  width: 6rpx;
					  background-color: #c24135;
					  margin-left: 10rpx;
				  }
				}
				textarea{
					font-size: 28rpx;
					color: #909090;
					height: 125rpx;
				}
				.place{
					font-size: 28rpx;
					color: #909090;
				}
			}
			&::-webkit-scrollbar{
				display: none;
			}
		}
		.footer{
			flex: 0 0 auto;
			padding: 24rpx 130rpx;
			button{
				@include button;
				background-color: #198AFA;
				color: #fff;
				height: 60rpx;
				line-height: 60rpx;
				font-size: 28rpx;
				width: 100%;
			}
		}
	}
</style>